/**
 *https://leetcode.cn/problems/construct-string-from-binary-tree/description/
 * Definition for a binary tree node.
 * struct TreeNode {
 *     int val;
 *     TreeNode *left;
 *     TreeNode *right;
 *     TreeNode() : val(0), left(nullptr), right(nullptr) {}
 *     TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
 *     TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}
 * };
 */
#include <vector>
#include <algorithm>
#include <iostream>
using namespace ::std;
struct TreeNode
{
    int val;
    TreeNode *left;
    TreeNode *right;
    TreeNode() : val(0), left(nullptr), right(nullptr) {}
    TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
    TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}
};
class Solution
{
public:
    string tree2str(TreeNode *root)
    {
        string s;
        if (root == nullptr)
        {
            return s;
        }

        s += to_string(root->val);

        if (root->left || root->right)
        {
            s += '(';
            s += tree2str(root->left);
            s += ')';
        }

        if (root->right)
        {
            s += '(';
            s += tree2str(root->right);
            s += ')';
        }

        return s;
    };
};